package com.neusoft.neuiotms.dcom.converter.change;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.neuiotms.dcom.dto.change.ChangeClosingDTO;
import com.neusoft.neuiotms.dcom.model.change.ChangeClosing;
import com.neusoft.neuiotms.dcom.repository.change.ChangeClosingRepository;

@Component
public class ChangeClosingConverter extends AbstractConverter<ChangeClosing, ChangeClosingDTO> {

    @Autowired
    private ChangeClosingRepository changeClosingRepository;

    @Override
    public ChangeClosing toModel(final ChangeClosingDTO dto) {
        ChangeClosing model;
        if (dto.isNew()) {
            model = new ChangeClosing();
        } else {
            model = this.changeClosingRepository.getOne(dto.getId());
        }
        model.setTaskId(dto.getTaskId());
        model.setWorkOrderNum(dto.getWorkOrderNum());
        model.setUpdateTime(new Date());
        return model;
    }

    @Override
    public ChangeClosingDTO toDTO(final ChangeClosing model, final boolean forListView) {
        final ChangeClosingDTO dto = new ChangeClosingDTO();
        dto.setTaskId(model.getTaskId());
        dto.setWorkOrderNum(model.getWorkOrderNum());
        dto.setId(model.getId());
        dto.setCreateTime(model.getCreateTime());
        dto.setUpdateTime(model.getUpdateTime());

        return dto;
    }

}
